package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advice.LogOperation;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResult<Student> studentSelect(StudentQueryParam studentQueryParam) {
        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());
        List<Student> studentList = studentMapper.studentSelect(studentQueryParam);
        Page p = (Page) studentList;
        PageResult<Student> pageResult = new PageResult<>();
        pageResult.setTotal(p.getTotal());
        pageResult.setRows(p.getResult());
        return pageResult;
    }

    @Override
    @LogOperation
    public void studentDelete(List<Integer> ids) {
        studentMapper.studentDelete(ids);
    }

    @Override
    @LogOperation
    public void studentInsert(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.studentInsert(student);
    }

    @Override
    public Student studentGetById(Integer id) {
        Student student = studentMapper.studentGetById(id);
        return student;
    }

    @Override
    @LogOperation
    public void studentUpdate(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.studentUpdate(student);
    }

    @Override
    @LogOperation
    public void studentDisciplinary(Integer id, Integer score) {
        studentMapper.studentDisciplinary(id,score);
    }
}
